package com.nbot.server.workers.impl;

import com.nbot.common.command.s2b.ReportAqyhCommand;
import com.nbot.common.command.s2b.ReportCommand;
import com.nbot.common.command.s2b.ReportFxglCommand;
import com.nbot.server.BotServer;
import com.nbot.server.domain.Orcalarm;
import com.nbot.server.domain.TAqyhAqyhgl;
import com.nbot.server.domain.TFxglykzDetail;
import com.nbot.server.domain.TbAqyhWtyh;
import com.nbot.server.properties.NbotProperties;
import com.nbot.server.service.OrcalarmService;
import com.nbot.server.service.TAqyhAqyhglService;
import com.nbot.server.service.TFxglykzDetailService;
import com.nbot.server.service.TbAqyhWtyhService;
import com.nbot.server.util.NbotStringUtil;
import com.nbot.server.util.ViewBeanChangeUtil;
import com.nbot.server.workers.OracleWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
//@Component("oracleWorker")
public class OracleWorkeFade implements OracleWorker {
    @Autowired
    private OrcalarmService orcalarmService;

    /**
     * 安全隐患服务
     */
    @Autowired
    private TbAqyhWtyhService tbAqyhWtyhService;


    /**
     * 安全隐患服务
     */
    @Autowired
    private TAqyhAqyhglService tAqyhAqyhglService;


    /**
     * 风险管理服务
     */
    @Autowired
    private TFxglykzDetailService tFxglykzDetailService;
    /**
     * 用于存储当前报警总数
     */
    private static int totalAlarms = 0;


    /**
     * 用于存储当前风险管理日志总数
     */
    private static int totalFxgls = 0;


    /**
     * 用于存储当前安全隐患日志总数
     */
    private static int totalAqyhs = 0;

    /**
     * 用于存储当前（旧）安全隐患日志总数
     */
    private static int totalAqyhOlds = 0;
    @Autowired
    private BotServer botServer;

    @Autowired
    private NbotProperties nbotProperties;

    @Override
    public void checkAlarmLog() {
//        orcalarmService

        Integer countFromDb = orcalarmService.getTodayCount();

        if (countFromDb == null) {
            //查询失败
            return;
        }
        if (countFromDb < 0) {
            //查询失败
            return;
        }
        if (countFromDb > totalAlarms) {
            // 当日报警数量大于totalAlarms 需要报警，报警后totalAlarms=countFromDb

            int retrunNmu = countFromDb - totalAlarms;//应该返回的条数
            //条数不能超过设定的最大值
            if (retrunNmu > nbotProperties.getMaxRentrnNum()) {
                retrunNmu = nbotProperties.getMaxRentrnNum();
            }
            List<Orcalarm> lists = orcalarmService.findLastN(retrunNmu);
            for (Orcalarm orcalarm : lists) {
                if (orcalarm != null) {
                    totalAlarms = countFromDb;
                    ReportCommand reportCommand = new ReportCommand();
                    reportCommand.setReportMsg("<oracle>[" + orcalarm.getPdesc() + "]");
                    reportCommand.setWarnLevel("报警类型为" + NbotStringUtil.getAlarmTypeStr(orcalarm.getAlarmType()) + "");
                    reportCommand.setValueNow("报警限制为" + orcalarm.getLimitValue() + ",报警值为" + orcalarm.getAlarmValue());
                    botServer.addCommand(reportCommand);
                }
            }
        } else if (countFromDb == totalAlarms) {
            // 当日报警数量等于totalAlarms 不需要报警。特殊情况countFromDb == totalAlarms==0 时也不需要报警
        } else {
            // 当日报警数量小于totalAlarms，出现这个情况一般为过了当天晚上12点，报警数量
            totalAlarms = countFromDb;
        }

    }

    @Override
    public void checkFxglLog() {


        ReportFxglCommand reportFxglCommand = new ReportFxglCommand();
        TFxglykzDetail obj=new TFxglykzDetail();
        obj.setBwtr("666");
        obj.setId("111");
        obj.setIshavbccs("setIshavbccs");
        obj.setJhzyrq(new Date());
        obj.setOrgid("setOrgid");
        obj.setJsazynrid("setJsazynrid");
        obj.setQrsj(new Date());
        obj.setTbr("tbr");
        obj.setZydd("zydd");
        reportFxglCommand.setFxgl(ViewBeanChangeUtil.toFxgl(obj));
        botServer.addCommand(reportFxglCommand);

    }

    @Override
    public void checkAqyhLog() {


        Integer countFromDb = tbAqyhWtyhService.getTodayCount();

        if (countFromDb == null) {
            //查询失败
            return;
        }
        if (countFromDb < 0) {
            //查询失败
            return;
        }
        if (countFromDb > totalAqyhs) {

            int retrunNmu = countFromDb - totalAqyhs;//应该返回的条数
            //条数不能超过设定的最大值
            if (retrunNmu > nbotProperties.getMaxRentrnNum()) {
                retrunNmu = nbotProperties.getMaxRentrnNum();
            }
            List<TbAqyhWtyh> lists = tbAqyhWtyhService.findLastN(retrunNmu);
            for (TbAqyhWtyh obj : lists) {
                if (obj != null) {
                    totalAqyhs = countFromDb;
                    ReportAqyhCommand reportAqyhCommand = new ReportAqyhCommand();
                    reportAqyhCommand.setAqyhType(ReportAqyhCommand.NEW_AQYH);
                    reportAqyhCommand.setAqyh(ViewBeanChangeUtil.toAqyh(obj));
                    botServer.addCommand(reportAqyhCommand);
                }
            }
        } else if (countFromDb == totalAqyhs) {
            // 当日报警数量等于totalAlarms 不需要报警。特殊情况countFromDb == totalAlarms==0 时也不需要报警
        } else {
            // 当日报警数量小于totalAlarms，出现这个情况一般为过了当天晚上12点，报警数量
            totalAqyhs = countFromDb;
        }

    }

    @Override
    public void checkAqyhOldLog() {
        Integer countFromDb = tAqyhAqyhglService.getTodayCount();

        if (countFromDb == null) {
            //查询失败
            return;
        }
        if (countFromDb < 0) {
            //查询失败
            return;
        }
        if (countFromDb > totalAqyhOlds) {

            int retrunNmu = countFromDb - totalAqyhOlds;//应该返回的条数
            //条数不能超过设定的最大值
            if (retrunNmu > nbotProperties.getMaxRentrnNum()) {
                retrunNmu = nbotProperties.getMaxRentrnNum();
            }
            List<TAqyhAqyhgl> lists = tAqyhAqyhglService.findLastN(retrunNmu);
            for (TAqyhAqyhgl obj : lists) {
                if (obj != null) {
                    totalAqyhOlds = countFromDb;
                    ReportAqyhCommand reportAqyhCommand = new ReportAqyhCommand();
                    reportAqyhCommand.setAqyhType(ReportAqyhCommand.OLD_AQYH);
                    reportAqyhCommand.setAqyhOld(ViewBeanChangeUtil.toAqyhOld(obj));
                    botServer.addCommand(reportAqyhCommand);
                }
            }
        } else if (countFromDb == totalAqyhOlds) {
            // 当日报警数量等于totalAlarms 不需要报警。特殊情况countFromDb == totalAlarms==0 时也不需要报警
        } else {
            // 当日报警数量小于totalAlarms，出现这个情况一般为过了当天晚上12点，报警数量
            totalAqyhOlds = countFromDb;
        }
    }
}
